package saucaca.Adapter;

import java.util.List;

import saucaca.Object.SongObject;
import saucaca.Util.AppController;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lemon.kenhnhacvn.BaseActivity;
import com.lemon.kenhnhacvn.BaseEngine;
import com.lemon.kenhnhacvn.R;

public class HomeVideo_HorizontalAdapter {
	private Context context;
	private List<SongObject> songs = null;
	LinearLayout linearGallery;

	ImageLoader imageLoader;

	// private ImageLoadingListener imageListener;

	public HomeVideo_HorizontalAdapter(Context c, LinearLayout linearGallery, List<SongObject> listSong) {
		context = c;
		this.linearGallery = linearGallery;
		try {
			this.linearGallery.removeAllViews();
		} catch (Exception e) {
		}
		this.songs = listSong;
		Show();
	}

	public void Show() {
		if (songs.size() > 1)
			for (int i = 0; i < songs.size() - 1; i += 2) {
				getView(i, linearGallery, true);
			}
		if (songs.size() % 2 == 0) {
		} else
			getView(songs.size() - 1, linearGallery, false);
	}

	public void getView(int i, LinearLayout layoutgallery, boolean check) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		ViewHolder holder, holder2;
		View convertView = inflater.inflate(R.layout.item_album_gridview, null);
		holder = new ViewHolder();
		holder.layout = (LinearLayout) convertView.findViewById(R.id.item_album_layout);
		holder.title = (TextView) convertView.findViewById(R.id.custom_view_album_Title);
		holder.artist = (TextView) convertView.findViewById(R.id.custom_view_album_artist);
		holder.image = (ImageView) convertView.findViewById(R.id.custom_view_album_image);
		holder.videoicon = (ImageView) convertView.findViewById(R.id.item_album_videoicon);
		holder.videoicon.setVisibility(View.VISIBLE);
		final SongObject item = songs.get(i);
		holder.title.setText(item.getTXNAME());
		holder.artist.setText(item.getNAME_SINGERS());
		BaseEngine.setVideoclick(context, holder.layout, item);
		holder.image.setLayoutParams(new LayoutParams(BaseActivity.linear_params_2.width, BaseActivity.linear_params_2.height));
		try {
			imageLoader=((BaseActivity)context).imageLoader;
		} catch (Exception e) {
			imageLoader=AppController.getInstance().getImageLoader();
		}
		
		imageLoader.get(item.getAVATAR(), ImageLoader.getImageListener(holder.image,
	         R.drawable.ic_default, R.drawable.ic_default));
		android.widget.LinearLayout.LayoutParams Params = new android.widget.LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		Params.setMargins(5, 5, 5, 5);
		convertView.setLayoutParams(Params);
		LinearLayout dialogLayout = new LinearLayout(context);
		dialogLayout.setOrientation(LinearLayout.VERTICAL);
		// dialogLayout.setBackgroundColor(Color.parseColor("#F3FFF0"));
		dialogLayout.setGravity(Gravity.CENTER);
		dialogLayout.addView(convertView);
		
		if (check) {
			View convertView2 = inflater.inflate(R.layout.item_album_gridview, null);
			holder2 = new ViewHolder();
			holder2.layout = (LinearLayout) convertView2.findViewById(R.id.item_album_layout);
			holder2.title = (TextView) convertView2.findViewById(R.id.custom_view_album_Title);
			holder2.artist = (TextView) convertView2.findViewById(R.id.custom_view_album_artist);
			holder2.image = (ImageView) convertView2.findViewById(R.id.custom_view_album_image);
			holder2.videoicon = (ImageView) convertView2.findViewById(R.id.item_album_videoicon);
			holder2.videoicon.setVisibility(View.VISIBLE);
			final SongObject item2 = songs.get(i + 1);
			holder2.title.setText(item2.getTXNAME());
			holder2.artist.setText(item2.getNAME_SINGERS());
			BaseEngine.setVideoclick(context, holder2.layout, item2);
			holder2.image.setLayoutParams(new LayoutParams(BaseActivity.linear_params_2.width, BaseActivity.linear_params_2.height));
			try {
				imageLoader=((BaseActivity)context).imageLoader;
			} catch (Exception e) {
				imageLoader=AppController.getInstance().getImageLoader();
			}
			
			imageLoader.get(item2.getAVATAR(), ImageLoader.getImageListener(holder2.image,
		         R.drawable.ic_default, R.drawable.ic_default));
			convertView2.setLayoutParams(Params);
			dialogLayout.addView(convertView2);
		}
		layoutgallery.addView(dialogLayout);
	}

	public class ViewHolder {
		LinearLayout layout;
		public TextView title;
		public TextView artist;
		public ImageView image,videoicon;
	}

}
